class IUP_EXPANDER
-- Creates a void container that can interactively show or hide its 
-- child.

inherit
	IUP_CONTAINER
		redefine
			execute_action,
			execute_openclose,
			execute_extrabutton
		end
	IUP_WIDGET_EXPAND
	IUP_WIDGET_TITLE
	IUP_WIDGET_WID
	IUP_WIDGET_FONT
	IUP_WIDGET_SIZE
	IUP_WIDGET_RASTERSIZE
	IUP_WIDGET_USERSIZE
	IUP_WIDGET_CLIENTSIZE
	IUP_WIDGET_CLIENTOFFSET
	IUP_WIDGET_POSITION
	IUP_WIDGET_MAXMIN_SIZE
	IUP_WIDGET_CHILD
	IUP_WIDGET_NAME
	IUP_WIDGET_CUSTOM_ATTRIBUTES

create {ANY}
	expander_empty,
	expander

feature {ANY}

	expander_empty
	   -- Create an empty expander.
		local
			p, a_expander: POINTER
		do
			a_expander := int_expander (p)
			set_widget(a_expander)
		end
	
	expander (child: IUP_WIDGET)
	   -- Create a new expander containing the widget.
		local
         a_expander: POINTER
		do
			a_expander := int_expander (child.widget)
			set_widget(a_expander)
		end

	-- Commands to handle attributes
	set_autoshow (state: BOOLEAN)
		-- (non inheritable): enables the automatic show of the child when mouse 
		-- is over the handler for more than 1 second. Default: No.
		do
			iup_open.set_attribute(Current, "AUTOSHOW", boolean_to_yesno(state))
		end

	is_autoshow: BOOLEAN
		-- Return the state of the autoshow.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "AUTOSHOW")
			Result := yesno_to_boolean(str)
		end

	set_animation (value: STRING)
		-- (non inheritable): enable animation during open/close. Works only for 
		-- BARPOSITION=TOP and does not works for AUTOSHOW. Also the child must 
		-- be a native container like IUP_TABS, IUP_FRAME, IUP_BACKGROUNBOX, or 
		-- IUP_SCROLLBOX, or it will not work accordantly. Values can be SLIDE 
		-- (child controls slide down), CURTAIN (child controls appears as if a 
		-- curtain is being pulled) or NO. Default: NO. 
		require
			is_valid_animation(value)
		do
			iup_open.set_attribute(Current, "ANIMATION", value)
		end

	get_animation: STRING
		-- Return the type of animation.
		do
			Result := iup_open.get_attribute(Current, "ANIMATION")
		end

	set_number_of_frames (num: INTEGER)
		-- Control the number of frames used for the animation, the default value 
		-- is 10 frames.
		require
			non_negative: num >= 0  
		do
			iup_open.set_attribute(Current, "NUMFRAMES", num.out)
		end

	get_number_of_frames: INTEGER
		-- Return the number of frames for the animation.
		local
			num: STRING
		do
			num := iup_open.get_attribute(Current, "NUMFRAMES")
			Result := num.to_integer
		end

	set_frame_time (time: INTEGER)
		-- Time between frames, the default value is 30ms (milliseconds). If the 
		-- dialog has lots of controls and its layout computation takes longer 
		-- than FRAMETIME, then a frame is lost, but the total animation time 
		-- (numframes*frametime) is always the same.
		require
			non_negative: time >= 0  
		do
			iup_open.set_attribute(Current, "FRAMETIME", time.out)
		end

	get_frame_time: INTEGER
		-- Return the frame time.
		local
			time: STRING
		do
			time := iup_open.get_attribute(Current, "FRAMETIME")
			Result := time.to_integer
		end

	set_rgb_back_color (red, green, blue: INTEGER)
		-- (non inheritable): background color of the bar handler. If not defined 
		-- it will use the background color of the native parent.
		do
			iup_open.set_attribute(Current, "BACKCOLOR", rgb_to_string(red, green, blue))
		end

	get_rgb_back_color: TUPLE[INTEGER, INTEGER, INTEGER]
		-- Return the RGB values of the back color.
		do
			Result := iup_open.get_rgb(Current, "BACKCOLOR")
		end

	set_bar_position (value: STRING)
		-- (creation only): indicates the bar handler position. Possible values 
		-- are "TOP", "BOTTOM", "LEFT" or "RIGHT". Default: "TOP".
		require
			is_valid_barposition(value)
		do
			iup_open.set_attribute(Current, "BARPOSITION", value)
		end

	get_bar_position: STRING
		-- Return the bar position.
		do
			Result := iup_open.get_attribute(Current, "BARPOSITION")
		end

	set_bar_size (size: INTEGER)
		-- (non inheritable): controls the size of the bar handler. Default: the 
		-- height or width that fits all its internal elements according to 
		-- BARPOSITION.
		require
			non_negative: size >= 0  
		do
			iup_open.set_attribute(Current, "BARSIZE", size.out)
		end

	get_bar_size: INTEGER
		-- Return the bar size.
		local
			size: STRING
		do
			size := iup_open.get_attribute(Current, "BARSIZE")
			Result := size.to_integer
		end

	set_extra_buttons (n: INTEGER)
		-- (non inheritable) (creation only): sets the number of extra image 
		-- buttons at right when BARPOSITION=TOP. The maximum number of buttons 
		-- is 3. See the EXTRABUTTON_CB callback. Default: 0.
		require
			n >= 0
			n <= 3
		do
			iup_open.set_attribute(Current, "EXTRABUTTONS", n.out)
		end

	get_extra_buttons: INTEGER
		-- Return the number of extrabuttons.
		local
			n: STRING
		do
			n := iup_open.get_attribute(Current, "EXTRABUTTONS")
			Result := n.to_integer
		end

	set_extra_button_id_image (imagename: STRING; id: INTEGER)
		-- Set the image name used for the button. id can be 1, 2 or 3. 1 is the 
		-- rightmost button, and count from right to left.
		require
			id >= 0
			id <= 3
		local
			str: STRING
		do
			str := "IMAGEEXTRA" + id.out
			iup_open.set_attribute(Current, str, imagename)
		end

	get_extra_button_id_image (id: INTEGER): STRING
		-- Return the name for the image at button number id.
		require
			id >= 0
			id <= 3
		local
			str: STRING
		do
			str := "IMAGEEXTRA" + id.out
			Result := iup_open.get_attribute(Current, str)
		end

	set_extra_button_id_image_press (imagename: STRING; id: INTEGER)
		-- Set the image name used for the button when pressed. id can be 1, 2 
		-- or 3. 1 is the rightmost button, and count from right to left.
		require
			id >= 0
			id <= 3
		local
			str: STRING
		do
			str := "IMAGEEXTRAPRESS" + id.out
			iup_open.set_attribute(Current, str, imagename)
		end

	get_extra_button_id_image_press (id: INTEGER): STRING
		-- Return the name for the image at button number id when pressed.
		require
			id >= 0
			id <= 3
		local
			str: STRING
		do
			str := "IMAGEEXTRAPRESS" + id.out
			Result := iup_open.get_attribute(Current, str)
		end

	set_extra_button_id_image_highlight (imagename: STRING; id: INTEGER)
		-- Set the image name for the button used when mouse is over the button 
		-- area. id can be 1, 2 or 3. 1 is the rightmost button, and count from 
		-- right to left.
		require
			id >= 0
			id <= 3
		local
			str: STRING
		do
			str := "IMAGEEXTRAHIGHLIGHT" + id.out
			iup_open.set_attribute(Current, str, imagename)
		end

	get_extra_button_id_image_highlight (id: INTEGER): STRING
		-- Return the image name for the button number id, used when mouse is 
		-- over the button area.
		require
			id >= 0
			id <= 3
		local
			str: STRING
		do
			str := "IMAGEEXTRAHIGHLIGHT" + id.out
			Result := iup_open.get_attribute(Current, str)
		end

	set_rgb_fore_color (red, green, blue: INTEGER)
		-- (non inheritable): title text color. Default: the global attribute 
		-- DLGFGCOLOR.
		do
			iup_open.set_attribute(Current, "FORECOLOR", rgb_to_string(red, green, blue))
		end
	
	get_rgb_fore_color: TUPLE[INTEGER, INTEGER, INTEGER]
		-- Return the RGB values of the fore color.
		do
			Result := iup_open.get_rgb(Current, "FORECOLOR")
		end

	set_rgb_open_color (red, green, blue: INTEGER)
		-- (non inheritable): title text color when STATE=OPEN. Defaults to the 
		-- FORECOLOR if not defined.
		do
			iup_open.set_attribute(Current, "OPENCOLOR", rgb_to_string(red, green, blue))
		end
	
	get_rgb_open_color: TUPLE[INTEGER, INTEGER, INTEGER]
		-- Return the RGB values of the open color.
		do
			Result := iup_open.get_rgb(Current, "OPENCOLOR")
		end

	set_rgb_high_color (red, green, blue: INTEGER)
		-- (non inheritable): title text color when highlighted. Works only when 
		-- TITLEEXPAND=Yes. Defaults to the FORECOLOR if not defined.
		do
			iup_open.set_attribute(Current, "HIGHCOLOR", rgb_to_string(red, green, blue))
		end
	
	get_rgb_high_color: TUPLE[INTEGER, INTEGER, INTEGER]
		-- Return the RGB values of the high color.
		do
			Result := iup_open.get_rgb(Current, "HIGHCOLOR")
		end

	set_image (imagename: STRING)
		--  (non inheritable): image name to replace the arrow image by a custom 
		--  image when STATE=CLOSE. Works only when BARPOSITION=TOP. Use 
		--  set_widget_name to associate an image to a name. See also 
		--  IUP_IMAGE.
		do
			iup_open.set_attribute(Current, "IMAGE", imagename)
		end

	get_image: STRING
		-- Return the name of the image when STATE=CLOSE.
		do
			Result := iup_open.get_attribute(Current, "IMAGE")
		end

	set_image_open (imagename: STRING)
		-- Set the image name used when STATE=OPEN.
		do
			iup_open.set_attribute(Current, "IMAGEOPEN", imagename)
		end

	get_image_open: STRING
		-- Return the image name used when STATE=OPEN.
		do
			Result := iup_open.get_attribute(Current, "IMAGEOPEN")
		end

	set_image_highlight (imagename: STRING)
		-- Set image name used when mouse is over the bar handler and STATE=CLOSE.
		do
			iup_open.set_attribute(Current, "IMAGEHIGHLIGHT", imagename)
		end

	get_image_highlight: STRING
		-- Return the image name used when mouse is over the bar handler and 
		-- STATE=CLOSE.
		do
			Result := iup_open.get_attribute(Current, "IMAGEHIGHLIGHT")
		end

	set_image_open_highlight (imagename: STRING)
		-- Set the image name used when mouse is over the bar handler and 
		-- STATE=OPEN.
		do
			iup_open.set_attribute(Current, "IMAGEOPENHIGHLIGHT", imagename)
		end

	get_image_open_highlight: STRING
		-- Return the image name used when mouse is over the bar handler and 
		-- STATE=OPEN.
		do
			Result := iup_open.get_attribute(Current, "IMAGEOPENHIGHLIGHT")
		end

	set_expanded
		-- Show the container elements.
		-- Setting this attribute will automatically change the layout of the 
		-- entire dialog so the child can be recomposed.
		do
			iup_open.set_attribute(Current, "STATE", "OPEN")
		end

	set_collapsed
		-- Hide the container elements.
		-- Setting this attribute will automatically change the layout of the 
		-- entire dialog so the child can be recomposed.
		do
			iup_open.set_attribute(Current, "STATE", "CLOSE")
		end

	is_expanded: BOOLEAN
		-- Return True if expanded.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "STATE")

			if str.is_equal("OPEN") then
				Result := True
			else
				Result := False
			end
		end

	set_state_refresh (state: BOOLEAN)
		-- (non inheritable): when state is changed "refresh" is automatically 
		-- called. Default: "True".
		do
			iup_open.set_attribute(Current, "STATEREFRESH", boolean_to_yesno(state))
		end

	is_state_refresh: BOOLEAN
		-- Return the value of staterefresh.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "STATEREFRESH")
			Result := yesno_to_boolean(str)
		end

	set_title_image (imagename: STRING)
		-- (non inheritable): title image, shown in the bar handler near the 
		-- expand/collapse button. When set it will reset TITLE (image and text 
		-- title are mutually exclusive). Shown only when BARPOSITION=TOP.
		do
			iup_open.set_attribute(Current, "TITLEIMAGE", imagename)
		end
	
	get_title_image: STRING
		-- Return the name of the title image.
		do
			Result := iup_open.get_attribute(Current, "TITLEIMAGE")
		end
	
	set_title_image_open (imagename: STRING)
		-- Set the title image name used when STATE=OPEN.
		do
			iup_open.set_attribute(Current, "TITLEIMAGEOPEN", imagename)
		end

	get_title_image_open: STRING
		-- Return the title image name used when STATE=OPEN.
		do
			Result := iup_open.get_attribute(Current, "TITLEIMAGEOPEN")
		end

	set_title_image_highlight (imagename: STRING)
		-- Set image name used when mouse is over the title image and STATE=CLOSE.
		do
			iup_open.set_attribute(Current, "TITLEIMAGEHIGHLIGHT", imagename)
		end

	get_title_image_highlight: STRING
		-- Return the image name used when mouse is over the title image and 
		-- STATE=CLOSE.
		do
			Result := iup_open.get_attribute(Current, "TITLEIMAGEHIGHLIGHT")
		end

	set_title_image_open_highlight (imagename: STRING)
		-- Set the image name used when mouse is over the title image and 
		-- STATE=OPEN.
		do
			iup_open.set_attribute(Current, "TITLEIMAGEOPENHIGHLIGHT", imagename)
		end

	get_title_image_open_highlight: STRING
		-- Return the image name used when mouse is over the title image and 
		-- STATE=OPEN.
		do
			Result := iup_open.get_attribute(Current, "TITLEIMAGEOPENHIGHLIGHT")
		end

	set_title_expand (state: BOOLEAN)
		-- (non inheritable): enable the expand/collapse action also at the 
		-- tile. Default: False.
		do
			iup_open.set_attribute(Current, "TITLEEXPAND", boolean_to_yesno(state))
		end

	is_title_expand: BOOLEAN
		-- Return the value of TITLEEXPAND.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "TITLEEXPAND")
			Result := yesno_to_boolean(str)
		end

	-- Callbacks

	-- Common
	set_cb_action (act: detachable FUNCTION[TUPLE[IUP_EXPANDER], STRING])
		-- Action generated when the element is activated. Affects each 
		-- element differently.
		local
			operation: INTEGER
		do
			cb_action := act

			if cb_action /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "ACTION", "Fn", operation)
		end

	-- Extra
	set_cb_open_close (act: detachable FUNCTION[TUPLE[IUP_EXPANDER, INTEGER], STRING])
		-- Action generated before the expander state is interactively 
		-- changed.
		-- ih: identifier of the element that activated the event. 
		-- state: new state to be applied.
		--
		-- Returns: if return IUP_IGNORE the new state is ignored.
		local
			operation: INTEGER
		do
			cb_openclose := act

			if cb_openclose /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "OPENCLOSE_CB", "NONEEDED", operation)
		end

	set_cb_extra_button (act: detachable FUNCTION[TUPLE[IUP_EXPANDER, INTEGER, INTEGER], STRING])
		-- Action generated when any mouse button is pressed or 
		-- released.
		--	ih: identifies the element that activated the event.
		-- button: identifies the extra button. can be 1, 2 or 3. (this is not 
		-- the same as BUTTON_CB)
		-- pressed: indicates the state of the button:
		-- 0 - mouse button was released;
		-- 1 - mouse button was pressed.
		local
			operation: INTEGER
		do
			cb_extrabutton := act

			if cb_extrabutton /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "EXTRABUTTON_CB", "NONEEDED", operation)
		end

	-- Validations

	is_valid_animation (value: STRING): BOOLEAN
		do
			if value.is_equal("SLIDE") or
				value.is_equal("CURTAIN") or
				value.is_equal("NO") then
				Result := True
			else
				Result := False
			end
		end

	is_valid_barposition (value: STRING): BOOLEAN
		do
			if value.is_equal("TOP") or
				value.is_equal("BOTTOM") or
				value.is_equal("LEFT") or
				value.is_equal("RIGHT") then
				Result := True
			else
				Result := False
			end
		end

feature {IUP}

	-- Internal handle of callbacks

	execute_action: STRING
		do
			if attached cb_action as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	execute_openclose (state: INTEGER): STRING
		do
			if attached cb_openclose as int_cb then
				Result := int_cb.item([Current, state])
			else
				Result := "IUP_DEFAULT"
			end
		end

	execute_extrabutton (button, pressed: INTEGER): STRING
		do
			if attached cb_extrabutton as int_cb then
				Result := int_cb.item([Current, button, pressed])
			else
				Result := "IUP_DEFAULT"
			end
		end

feature {NONE}

	-- For callbacks
	cb_action: detachable FUNCTION[TUPLE[IUP_EXPANDER], STRING]
	
	cb_openclose: detachable FUNCTION[TUPLE[IUP_EXPANDER, INTEGER], STRING]
	cb_extrabutton: detachable FUNCTION[TUPLE[IUP_EXPANDER, INTEGER, INTEGER], STRING]

	-- Internals

	int_expander (child: POINTER): POINTER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return IupExpander ($child);"
      end

end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2019, 2020 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
